package com.glela.packet.util;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.IntStream;

public class RedPacketUtil {

    private static RedPacketUtil instance = new RedPacketUtil();

    private static String DEFAULT_PATTERN = "yyyyMMddHHmmss";

    public static char[] charArray = "abcVWXxyzYdef234jkrst178uvwAghiBqQRSDE56FGHInopJ9OPKLClmMN0TUZ".toCharArray();

    private static LongAdder count = new LongAdder();

    private RedPacketUtil() {
    }

    public static RedPacketUtil getInstance() {
        return instance;
    }


    public static String generateShareKey(int shareUserId, int redPacketId) {

        StringBuilder shareKey = new StringBuilder();

        int totalLen = String.valueOf(shareUserId).length() + String.valueOf(redPacketId).length();

        int limitLen = 12;

        char[] shareUserIdArray = String.valueOf(shareUserId).toCharArray();
        char[] redPacketIdArray = String.valueOf(redPacketId).toCharArray();

        IntStream.range(0, String.valueOf(shareUserId).length()).forEach(i -> {
            int index = Integer.valueOf(String.valueOf(shareUserIdArray[i]));
            shareKey.append(charArray[index]);
        });
        IntStream.range(0, String.valueOf(redPacketId).length()).forEach(j -> {
            int index = Integer.valueOf(String.valueOf(redPacketIdArray[j]));
            shareKey.append(charArray[index]);
        });

        if (totalLen < limitLen) {
            IntStream.range(0, limitLen - totalLen).forEach(k -> {
                int index = (int) Math.ceil(Math.random() * charArray.length - 1);
                shareKey.append(charArray[index]);
            });
        }
        return shareKey.toString();
    }


    public static String generateRandom(int len) {
        StringBuilder str = new StringBuilder();
        IntStream.range(0, len).forEach(k -> {
            int index = (int) Math.ceil(Math.random() * charArray.length - 1);
            str.append(charArray[index]);
        });
        return str.toString();
    }


    public static String generateRedPacketCode(String prefix, String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        if (StringUtils.isBlank(prefix)) {
            prefix = "NO";
        }
        LocalDateTime ldt = LocalDateTime.now();
        return prefix + "-" + DateTimeFormatter.ofPattern(pattern).format(ldt)
                + "" + getCountCode();
    }

    public static String generateRedPacketCode(String prefix) {
        return RedPacketUtil.getInstance().generateRedPacketCode(prefix, null);
    }

    private static String getCountCode() {
        StringBuilder result = new StringBuilder();
        count.add(1);
        long num = count.longValue();
        if (num >= 10000) {
            result.append("0001");
            count.reset();
        } else {
            IntStream.range(0, 4 - String.valueOf(num).length()).forEach(i -> result.append("0"));
            result.append(num + "");
        }
        return result.toString();
    }
}
